
local locplayer = ac.player.self
local w,h = 800,536
local ui = class.panel.create([[UI\human-tooltip-background.blp]],(1920 - w)/2,200,w,h)
ui:hide()
local item = ui:add_panel('',200,0,ui.w-200,ui.h)
ui.count = item:add_text('吞噬(10)',0,0,nil,50,14,5)
ui:set_level(99)

local normal_icon = [[ui\ui_b0.tga]]
local w1,h1 = 64,64
ui.panels = {}
for a=1,6 do
    for b=1,8 do
        local x = (b-1) * (w1+4) + 15
        local y = (a-1) * (h1+4) + 30 + 40
        local panel = item:add_panel(normal_icon,x,y,w1,h1)

        panel.icon = panel:add_button('',0,0,w1,h1,1)
        
        panel.icon.frame = panel.icon:add_panel('')
        panel.icon.overlay = panel.icon:add_number_panel((w1-36)/2,h1/2,36,32)
        panel.icon.overlay:hide()

        panel.tfcard = panel:add_panel([[core\bantouming.blp]],5,5,25,15)
        panel.tfcard.text = panel.tfcard:add_text('名称',0,0,0,nil,8,4)

        panel.icon.index = i
        panel.back = panel:add_panel([[textures\black32.blp]])
        panel.back:hide()
        panel.back:set_alpha(0.7)
        
        local index = #ui.panels + 1
        panel.icon.index = index
        ui.panels[index] = panel
    end
end



--菜单
local mus = {'卡牌吞噬','装备吞噬','技能吞噬'}
local tab = {'天赋显示表','装备吞噬列表','技能吞噬列表'}

for a,name in ipairs(mus) do
    local button = ui:add_namebutton(name,15,50 + (a-1)*51,150,50)
    button.btnty = '菜单按钮'
    button.tab_name = tab[a]
    if a==1 then
        ac.wait(10,function()
            ui:on_button_clicked(button)
        end)
    end
end



ui.select = ui:add_highlight([[UI\Widgets\Console\Human\CommandButton\human-activebutton.blp]],0,0,w1,h1)
ui.select:hide()

local empty = {}
function ui:update()
    self.show_data = locplayer:get_data(self.tab_name)
    local data = self.show_data or empty
    local index = (self.page - 1) * #self.panels
    self.max_page = math.max(math.ceil(#data/#self.panels),1)
    self.page_text:set_text(('%s/%s'):format(self.page,self.max_page))
    self.count:set_text(('吞噬|cff999999(|r|cff00ffff%s|r|cff999999)|r'):format(#data))
    for i,panel in ipairs(self.panels) do
        local button = panel.icon
        local skill = data[i + index]
        button.skill = skill
        if skill then
            button.frame:set_normal_image(skill.frame_art)
            button:set_normal_image(skill.art)
            if skill.card_count then
                button.overlay.text:set_text(skill.card_count)
                button.overlay:show()
            else
                button.overlay:hide()
            end
            if skill.show_class_name then
                panel.tfcard.text:set_text(skill.show_class_name)
                panel.tfcard:set_width(panel.tfcard.text:get_width()+5)
                panel.tfcard:show()
            else
                panel.tfcard:hide()
            end
        else
            panel.tfcard:hide()
            button.overlay:hide()
            button:set_normal_image('')
            button.frame:set_normal_image('')
        end
    end
end

local empty = {}
function ui:lock_class(name)
    local data = self.show_data or empty
    local index = (self.page - 1) * #self.panels
    for i,panel in ipairs(self.panels) do
        local button = panel.icon
        local skill = data[i + index]
        if name==nil or skill==nil or (skill and skill.show_class==name) then
            panel.back:hide()
        else
            panel.back:show()
        end
    end
end

function ui:on_button_mouse_enter(button)
    local skill = button.skill
    if skill then
        local x,y = button:get_real_position()
        self.select:set_real_position(x,y)
        self.select:show()
        self:lock_class(skill.show_class)
        local tip = format_obj(skill,skill.ts_tip or skill.tip)
        local title = format_obj(skill,skill.jb_title or skill.title or skill.name)
        self:tooltip(title,tip)
    end
end

function ui:on_button_mouse_leave(button)
    self.select:hide()
    self:lock_class()
end

function ui:on_button_clicked(button)
    if button==self.page_add then
        self.page = math.min(self.page + 1,self.max_page)
        self:update()
    elseif button==self.page_los then
        self.page = math.max(self.page - 1,1)
        self:update()
    elseif button.btnty=='菜单按钮' then
        local name = button.name
        if self.select_map then
            self.select_map.text:set_color(0xffffffff)
        end
        self.select_map = button
        self.select_map.text:set_color(0xff00ff00)
        self.tab_name = button.tab_name
    end
end


--[翻页]
ui:add_page_area((ui.w + 100)/2,ui.h - 45)

ac.game:event '玩家-打开吞噬界面'(function(_,player)
    if player==locplayer then
        if ui:is_show() then
            ui:hide()
            return
        end
        ui.page = 1
        ui:update()
        ui:show()
    end
end)

ac.game:notify('游戏-注册显示UI',ui,true)

return button